package com.seo.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;

/**
 * HttpClientManagerT
 * 
 * @author Administrator 说明：日期处理 创建人：FH Q313596790
 * @version 2017.12.01 11:40
 */
public class HttpClientManagerT {

	/**
	 * 获取数据
	 * 
	 * @param url url
	 * @return 返回值
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String getData(String url) {
		HttpGet httpget = new HttpGet(url);

		return getData(httpget, null);
	}

	/**
	 * 获取数据
	 * 
	 * @param url url
	 * @return 返回值
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String getData(String url, Map<String, String> headers) {
		HttpGet httpget = new HttpGet(url);

		return getData(httpget, headers);
	}

	/**
	 * 获取数据
	 * 
	 * @param url url
	 * @return 返回值
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String getDatas(String url) {
		HttpGet httpget = new HttpGet(url);

		return getDatas(httpget);
	}

	/**
	 * 获取数据
	 * 
	 * @param httpget httpget
	 * @return 返回值
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String getData(HttpGet httpget, Map<String, String> headers) {
		CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();

		String res = null;
		CloseableHttpResponse response = null;
		try {
			RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(3000)
					.setSocketTimeout(3000).setConnectTimeout(3000).build();
			httpget.setConfig(requestConfig);
			if (headers != null) {
				for (Map.Entry<String, String> entry : headers.entrySet()) {
					httpget.setHeader(entry.getKey(), entry.getValue());
				}

			}

			response = httpClient.execute(httpget);
			InputStream in = response.getEntity().getContent();
			res = IOUtils.toString(in, "UTF-8");
			in.close();
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return res;
	}

	/**
	 * 获取数据
	 * 
	 * @param httpget httpget
	 * @return 返回值
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String getDatas(HttpGet httpget) {
		CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();

		String res = null;
		CloseableHttpResponse response = null;
		try {
			RequestConfig requestConfig = RequestConfig.custom().build();
			httpget.setConfig(requestConfig);
			response = httpClient.execute(httpget);
			InputStream in = response.getEntity().getContent();
			res = IOUtils.toString(in, "UTF-8");
			in.close();
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return res;
	}

	/**
	 * 获取数据
	 * 
	 * @param url      url
	 * @param xmlParam xmlParam
	 * @return 返回值
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String postData(String url, String xmlParam) {

		HttpPost httpPost = new HttpPost(url);

		return postData(httpPost, xmlParam);
	}

	public static String postData(String url, Map<String, String> params, Map<String, String> headers) {

		HttpPost httpPost = new HttpPost(url);

		return postData(httpPost, params, headers);
	}

	public static String postData(HttpPost httpPost, Map<String, String> params, Map<String, String> headers) {
		CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();

		String res = null;
		CloseableHttpResponse response = null;
		try {
			RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(3000)
					.setSocketTimeout(3000).setConnectTimeout(3000).build();
			httpPost.setConfig(requestConfig);
			if (headers != null) {
				for (Map.Entry<String, String> entry : headers.entrySet()) {
					httpPost.setHeader(entry.getKey(), entry.getValue());
				}
			}

			if (params != null) {
				List<NameValuePair> paramList = new ArrayList<>();
				for (Map.Entry<String, String> entry : params.entrySet()) {
					paramList.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
				}
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "UTF-8");
				httpPost.setEntity(entity);
			}

			response = httpClient.execute(httpPost);

			InputStream in = response.getEntity().getContent();
			res = IOUtils.toString(in, "UTF-8");
			in.close();
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return res;
	}

	/**
	 * 获取数据
	 * 
	 * @param httpPost httpPost
	 * @param xmlParam xmlParam
	 * @return 返回值
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String postData(HttpPost httpPost, String xmlParam) {
		CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();

		String res = null;
		CloseableHttpResponse response = null;
		try {
			httpPost.setEntity(new StringEntity(xmlParam, "UTF-8"));
			response = httpClient.execute(httpPost);

			InputStream in = response.getEntity().getContent();
			res = IOUtils.toString(in, "UTF-8");
			in.close();
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return res;
	}

	/**
	 * 获取数据
	 * 
	 * @param httpPost httpPost
	 * @return 返回值
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String postData(HttpPost httpPost) {
		CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();

		String res = null;
		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(httpPost);

			InputStream in = response.getEntity().getContent();
			res = IOUtils.toString(in, "UTF-8");
			in.close();
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return res;
	}

	/**
	 * 获取数据
	 * 
	 * @param url      url
	 * @param params   params
	 * @param xmlParam xmlParam
	 * @param factory  factory
	 * @return 返回值
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String postSslData(String url, Map<String, String> params, String xmlParam,
			LayeredConnectionSocketFactory factory) {

		HttpPost httpPost = new HttpPost(url);
		if (params != null) {
			for (String k : params.keySet()) {
				httpPost.addHeader(k, params.get(k));
			}
		}

		return postSslData(httpPost, xmlParam, factory);
	}

	/**
	 * 获取数据
	 * 
	 * @param httpPost httpPost
	 * @param xmlParam xmlParam
	 * @param factory  factory
	 * @return 返回值
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String postSslData(HttpPost httpPost, String xmlParam, LayeredConnectionSocketFactory factory) {
		CloseableHttpClient httpClient = HttpClientUtil.getHttpClient(factory);

		String res = null;
		CloseableHttpResponse response = null;
		try {
			httpPost.setEntity(new StringEntity(xmlParam, "UTF-8"));
			response = httpClient.execute(httpPost);

			InputStream in = response.getEntity().getContent();
			res = IOUtils.toString(in, "UTF-8");
			in.close();
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return res;
	}

	/**
	 * 获取数据
	 * 
	 * @param url url
	 * @return 返回值
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static HttpPost getPostMethod(String url) {
		HttpPost pmethod = new HttpPost(url); // 设置响应头信�?
		pmethod.addHeader("Connection", "keep-alive");
		pmethod.addHeader("Accept", "*/*");
		pmethod.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		pmethod.addHeader("X-Requested-With", "XMLHttpRequest");
		pmethod.addHeader("Cache-Control", "max-age=0");
		pmethod.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");

		return pmethod;
	}

	public static String APISTORE(String param) {

		String returnStr = null; // 返回结果定义
		URL url = null;
		HttpURLConnection httpURLConnection = null;
		try {
			url = new URL("http://aliyun-idphotos-verify.apistore.cn/idcardPhoto" + "?" + param);
			httpURLConnection = (HttpURLConnection) url.openConnection();
			httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
			httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			httpURLConnection.setRequestProperty("Authorization", "APPCODE " + "15bb490e012242bdbbb0c2c1ea0e17ff");
			httpURLConnection.setDoOutput(true);
			httpURLConnection.setDoInput(true);
			httpURLConnection.setRequestMethod("GET");
			httpURLConnection.setUseCaches(false); // 不用缓存
			httpURLConnection.connect();
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
			StringBuffer buffer = new StringBuffer();
			String line = "";
			while ((line = reader.readLine()) != null) {
				buffer.append(line);
			}

			reader.close();
			returnStr = buffer.toString();
		} catch (Exception e) {
			e.printStackTrace();

			return null;
		} finally {
			if (httpURLConnection != null) {
				httpURLConnection.disconnect();
			}
		}
		return returnStr;
	}

	public static void main(String[] args) {
		String param = "realName=任同超&cardNo=130423199304074030";
		System.out.println(APISTORE(param));
	}

}
